package javaapplication3;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;



public class HttpConnection implements Runnable {
	
	private static final String TAG = HttpConnection.class.getName(); 

	private static final int GET = 0;
	private static final int POST = 1;
	
	private String url;
	private int method;
	private Map<String, String> params;
	private Map<String, String> headers;
	private HttpClient httpClient;
	private HttpResponse response;
	
	public HttpConnection(DefaultHttpClient httpClient) {
		this.httpClient = httpClient;
	}

	public void create(int method, String url, Map<String, String> headers, Map<String, String> params) {
		this.method = method;
		this.url = url;
		this.headers = headers;
		this.params = params;
                run();
	}
	
	public void get(String url, Map<String, String> headers) {
		create(GET, url, headers, null);
	}

	public void get(String url) {
		create(GET, url, null, null);
	}

	public void post( String url, Map<String, String> headers, Map<String, String> params) {
		create(POST, url, headers, params);
	}
	
	public void post( String url, Map<String, String> params) {
		create(POST, url, null, params);
	}

	public void run() {
		try {
			switch (method) {
			case POST:
				HttpPost post = new HttpPost(url);
				setHeaders(post);
				setParams(post);
				response = httpClient.execute(post);
				break;
			case GET:
				HttpGet get = new HttpGet(url);
				setHeaders(get);
				response = httpClient.execute(get);
				break;
			default:
				throw new AssertionError("");
			}
		} catch (ClientProtocolException e) {
			System.out.println("ClientProtocolException: " + e.getMessage());
		} catch (IOException e) {
			System.out.println("IOException" + e.getMessage());
		} catch (IllegalStateException e) {
			System.out.println("IllegalStateException" + e.getMessage());
		} 
	}
	
	private void setHeaders(HttpUriRequest uriRq){
		if (headers != null && !headers.isEmpty()) {
			for (Map.Entry<String, String> entry : headers.entrySet()) {
				uriRq.addHeader(entry.getKey(), entry.getValue());
			}
		}
	}
	
	private void setParams(HttpPost post) throws UnsupportedEncodingException {
		if (params != null && !params.isEmpty()) {
			List<NameValuePair> postParams = new ArrayList<NameValuePair>();

			for (Map.Entry<String, String> entry : params.entrySet()) {
				postParams.add(new BasicNameValuePair(entry.getKey(), entry
						.getValue()));
			}

			post.setEntity(new UrlEncodedFormEntity(postParams, HTTP.UTF_8));
		}		
	}
	
	public HttpEntity getEntity(){
		return response.getEntity();
	}
}
